import React, {useEffect, useState} from "react";
import {Card, Form, Input, Select, Switch} from "antd";
import baseInformationClass from "@/components/properties-panel-extension/components/index.module.css";

import {
    ASSIGNEETYPE, DOCUMENTATION,
    FLowCustomNameList
} from "@/components/properties-panel-extension/config/CustomName.ts"

interface MyComponentProps {
    bpmnInstance: any,
}

interface DefaultOptionType {
    value: string;
    label: string;
}

// 任务配置组件
const ServiceConfigurationView: React.FC<MyComponentProps> = (props: any) => {
        const {bpmnInstance} = props
        const {modeling, bpmnElement = {}, modeler} = bpmnInstance
        const [baseInfo, setBaseInfo] = useState<any>();
        useEffect(() => {
            if (bpmnElement.businessObject) {
                const businessObject = bpmnElement.businessObject
                setBaseInfo({
                    ...bpmnElement.businessObject,
                    implementation: businessObject.implementation || '',
                    async: businessObject.async || false,
                    implementationType: businessObject.class
                        ? 'class'
                        : businessObject.expression
                            ? 'expression'
                            : 'delegateExpression'
                })

            } else {
                setBaseInfo({
                    ...bpmnElement.businessObject,
                    implementation: '',
                    async: false,
                    implementationType: 'class'
                })
            }

        }, [bpmnElement.businessObject]);

        // 改变配置信息
        const handleServiceChange = (value: any, key: string) => {
            console.log(value, key)
            setBaseInfo({...baseInfo, [key]: value});
            switch (key) {
                case "implementation":
                    modeling.updateProperties(bpmnElement, {"implementation": value || undefined});
                    break;
                case "async":
                    modeling.updateProperties(bpmnElement, {"flowable:async": value || undefined});
                    break;
            }
        };
        // const handleServiceChange = () => {
        //     if (!bpmnElement || !modeler) return
        //
        //     modeling.updateProperties(props.currentElement, {
        //         implementation: serviceConfig.implementation,
        //         async: serviceConfig.async,
        //     })
        // }

        const assigneeTypeList: DefaultOptionType[] = [
            {
                value: "class",
                label: 'Java类',
            }, {
                value: "expression",
                label: '表达式',
            },
            {
                value: "delegateExpression",
                label: '代理表达式',
            },
        ]
        const assigneeTypeMap = assigneeTypeList.reduce((acc, item) => {
            acc[item.value] = item.label;
            return acc;
        }, {} as Record<string, string>);
        return (
            <>
                <Card
                    className={baseInformationClass.CardClass}
                    title={`任务配置`}
                    extra={null}>
                    <Form>
                        <Form.Item
                            label="实现类型"
                            labelCol={{span: 6}}
                            wrapperCol={{span: 18}}>
                            <Select
                                value={[baseInfo?.implementationType]}
                                defaultValue={[baseInfo?.implementationType]}
                                style={{width: '80%'}}
                                options={assigneeTypeList}
                                onChange={(e) => handleServiceChange(e, "implementationType")}
                            />
                        </Form.Item>
                        <Form.Item
                            label={`实现值`}
                            labelCol={{span: 6}}
                            wrapperCol={{span: 18}}>
                            <Input
                                value={baseInfo?.implementation}
                                onChange={(e) => handleServiceChange(e.target.value, "implementation")}
                            />
                        </Form.Item>
                        <Form.Item
                            label={`异步执行`}
                            labelCol={{span: 6}}
                            wrapperCol={{span: 18}}>
                            <Switch value={baseInfo?.async} checkedChildren="开启" unCheckedChildren="关闭" defaultChecked
                                    onChange={(e) => handleServiceChange(e, "async")}/>
                        </Form.Item>

                    </Form>
                </Card>


            </>

        );
    }
;

export default ServiceConfigurationView;
